package com.travel.Service.Impl;

import com.alipay.api.domain.OutboundOrder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.travel.Bean.Strategy;
import com.travel.Dao.StrategyMapper;
import com.travel.JPA.StrategyJPA;
import com.travel.util.ConsoleOutputControl;
import com.travel.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.bind.ValidationEvent;
import java.util.List;

@Service
public class StrategyServiceImpl {

    @Autowired
    private StrategyJPA strategyJPA;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private RedisUtil redisUtil;

    private final String valName = "Travel_strategy";

    @Cacheable(value = "Travel_Strategy_List",key = "(null != #cultureId ? #cultureId : #ticketId)")
    public PageInfo<Strategy> getStrategyList(Integer ticketId,Integer cultureId,String strategyTitle,Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Strategy> strategies = strategyMapper.getStrategyList(ticketId,strategyTitle,cultureId);

        return new PageInfo<>(strategies);
    }

    /**
     * 保存攻略
     *
     * @param strategy 攻略
     * @return boolean
     */
    @CacheEvict(value = "Travel_Strategy_List",key = "#strategy.ticket_id")
    public boolean saveStrategy(Strategy strategy){
        try{
            strategyJPA.save(strategy);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 找到攻略通过id
     *
     * @param strategyId strategy id
     * @return {@link Strategy}
     */
    @Cacheable(value = "Traver_strategy_id",key = "#strategyId")
    public Strategy findStrategyById(Integer strategyId){
        Strategy strategy = new Strategy();
        try{
            strategy = strategyJPA.findById(strategyId).get();
        }catch (Exception e){
            e.printStackTrace();
            ConsoleOutputControl.getOutputString_33("findstrategyById error");
        }
        return strategy;
    }


    /**
     * 攻略向上
     * 攻略点赞
     * strategy点赞
     *
     * @param strategyId 攻略id
     * @param valUp      薇尔了
     * @return boolean
     */
    @Transactional
    public boolean strategy_up(Integer strategyId,Integer valUp){
        String key = valName+":"+strategyId+":up";
        Integer upNum = 0;
        try {
//            查询攻略的点赞数量
            upNum = this.findStrategy_up(strategyId);
            upNum+=valUp;
            //对数据库中的点赞数据进行更新
            strategyJPA.strategyUp(strategyId, upNum.toString());
            //对redis中的点赞数据进行更新
            redisUtil.update(key,upNum.toString());
        }catch (Exception e){
            e.printStackTrace();
            ConsoleOutputControl.getOutputString_33("strategy_up error");
            return false;
        }
        return true;
    }


    /**
     * 找到攻略向上
     *
     * @param strategyId 攻略id
     * @return {@link Integer}
     */
    public Integer findStrategy_up(Integer strategyId){
        String key = valName+":"+strategyId+":up";
        String upNum = null;
        //查询攻略的点赞数量
        try{
            if(redisUtil.persist(key))
                upNum = redisUtil.get(key);
            else
                upNum = strategyJPA.findStrategy_upById(strategyId);
            if(null == upNum)
                return 0;
            return Integer.parseInt(upNum);
        }catch (Exception e){
            e.printStackTrace();
            ConsoleOutputControl.getOutputString_33("findStrategy_up error");
            return -1;
        }

    }



}
